<?php
	/**
	 * 公共上传类
	 */
	class UploadfileAction extends CommonAction {
		public function upfile(){
			$type = array('jpg','gif','png','jpeg');
			self::commupload($type);
		}
		
		public function uploadflash(){
			$type = array('swf','flv');
			self::commupload($type);
		}
		
		public function uploadlink(){
			$type = array('zip','rar','7z','gz');
			self::commupload($type);
		}
		
		public function uploadmov(){
			$type = array('wmv','avi','wma','mp3','mid');
			self::commupload($type);
		}
		
		public function uploadvideo(){
			if ( !empty( $_FILES ) ){
				if ( isset( $_SESSION['video_info'] ) ) {
					$o_file = $_SESSION['video_info']['savepath'] . $_SESSION['video_info']['savename'];
					if ( file_exists( $o_file ) ){
						unlink( $o_file );
					}
					unset( $_SESSION['video_info'] );
				}
				$type = array('mp4','wmv','flv','f4v','3gp');
				$this->commupload($type,'video');
			}else{
				$this->error('上传失败');
			}
			
		}
		
		private function commupload($type=array(),$succ=''){
			if ( !empty( $succ ) && $succ = 'video' ){
				$uploadsize = 102400000;
			}else{
				$uploadsize = 3145728;
			}
			import("ORG.Net.UploadFile");
			$upload = new UploadFile();
			$upload->savePath = $_SERVER['DOCUMENT_ROOT'] . "/".C('WEB_PUBLIC_PATH')."/".C('DIR_ATTCH_PATH')."/";
			$upload->maxSize = $uploadsize;
			$upload->allowExts = $type;
			$upload->saveRule = 'uniqid';
			$upload->autoSub = true;
			$upload->subType = 'date';
			$upload->dateFormat = "Ymd";
			if(!$upload->upload()){
				$err = $this->error($upload->getErrorMsg());
			}else{
				$info = $upload->getUploadFileInfo();
			}
			if ( empty( $succ ) ){
				$msg="{'url':'".__ROOT__."/".C('WEB_PUBLIC_PATH')."/".C('DIR_ATTCH_PATH').'/'.$info[0]['savename']."','localname':'".$info[0]['name']."','id':'1'}";
				echo "{'err':'".$err."','msg':".$msg."}";
			}else{
				if ( $succ == 'video' ){
					$_SESSION['video_info'] = $info[0];
					$this->ajaxReturn( 1,1,1 );
				}
			}
		}
		
		public function uploadeditor(){
			import("ORG.Net.Keditor");
	       	$ke = new Keditor();
	       	$root_path = $_SERVER['DOCUMENT_ROOT'] . "/" . C('WEB_PUBLIC_PATH') . "/" . C('DIR_ATTCH_PATH') . "/";
			$root_url = __ROOT__ . '/' . C('WEB_PUBLIC_PATH') . '/' . C('DIR_ATTCH_PATH') . "/";
			$ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
	       	$filesize = 409600;
	       	$ke->upload($root_path,$root_url,$ext_arr,$filesize);
		}
		
		
		public function removefile(){
			if ( !empty( $_POST['file'] ) ){
				if ( $_POST['video_info'] ){
					$file = $_POST['file'];
					unset( $_SESSION['video_info'] );
				}else{
					$file = $_SERVER['DOCUMENT_ROOT'] . $_POST['file'];
				}
				if ( file_exists( $file ) ){
		    		@unlink( $file );
		    	}
				$this->ajaxReturn(1,1,1);
			}else{
				$this->ajaxReturn(0,0,0);
			}
		}
	}
	